package com.yanchangapp;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yanchangapp.adapter.PsCntCreateListAdapter;
import com.yanchangapp.model.bean.ProductExtractBean;
import com.yanchangapp.widget.AmountView;
import com.yanchangapp.widget.ExpandLayout;
import com.zxl.baselib.util.formatter.BigDecimalUtils;

import java.util.List;

/**
 * Created by apple on 2019-08-14.
 * description:
 */
public class PsCreateListAdapter extends BaseQuickAdapter<ProductExtractBean, BaseViewHolder> {

    private ITotalAmountChanged mITotalAmountChanged;

    public void setITotalAmountChanged(ITotalAmountChanged ITotalAmountChanged) {
        mITotalAmountChanged = ITotalAmountChanged;
    }

    public interface ITotalAmountChanged{
        void justCalculateTotalPrice();
    }


    public PsCreateListAdapter(int layoutResId, @Nullable List<ProductExtractBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ProductExtractBean item) {
        helper.setText(R.id.tvProductName,item.getProductName());
        helper.setText(R.id.tvSinglePrice, BigDecimalUtils.keep2Decimal(item.getPrice()));
        helper.setText(R.id.tvProductNo,item.getProductNo());
        helper.setText(R.id.tvProductFormat,item.getProductFormat());
        helper.setText(R.id.tvRegistratNoName,item.getRegistratNoName());
        helper.setText(R.id.tvRegistratNo,item.getRegistratNo());
        helper.setText(R.id.tvManufacturer,item.getManufacturer());
        ImageView ivArrow = helper.getView(R.id.ivArrow);
        // 设置图片...
        ivArrow.setImageResource(R.mipmap.ic_order_arrow_down);
        LinearLayout llArrow = helper.getView(R.id.llArrow);
        ExpandLayout expandLayout = helper.getView(R.id.expandLayout);
        AmountView amountView =  helper.getView(R.id.amountView);
        amountView.setNowShowText(item.getSelectSize());
        amountView.setOnAmountChangeListener(new AmountView.AmountChangeListener() {
            @Override
            public void onAmountChanged(int count) {
                item.setSelectSize(count);
                if (mITotalAmountChanged != null) {
                    mITotalAmountChanged.justCalculateTotalPrice();
                }
            }
        });

        if (expandLayout != null) {
            expandLayout.initExpand(false,ivArrow);
        }

        llArrow.setOnClickListener(new MyExpandListener(expandLayout));
        // 添加删除点击事件...
        helper.addOnClickListener(R.id.ivDelete).addOnClickListener(R.id.ivEdit).addOnClickListener(R.id.tvSinglePrice);
    }

    private class MyExpandListener implements View.OnClickListener{
        private ExpandLayout mExpandLayout;

        public MyExpandListener(ExpandLayout expandLayout) {
            mExpandLayout = expandLayout;
        }

        @Override
        public void onClick(View v) {
            mExpandLayout.toggleExpand();
        }
    }
}
